package com.questetra.bpms.example.select.controller.country;

import java.util.List;
import java.util.Locale;

import org.datanucleus.util.StringUtils;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.questetra.bpms.example.select.model.Country;
import com.questetra.bpms.example.select.service.CountryService;

public class IndexController extends Controller {
    private CountryService service = new CountryService();

    @Override
    public Navigation run() throws Exception {
        String query = request.getParameter("query");
        String[] values = request.getParameterValues("values");
        String sLocale = request.getParameter("locale");
        Locale locale = Locale.ENGLISH;
        if (!StringUtils.isWhitespace(sLocale)) {
            locale = new Locale(sLocale.trim());
        }
        List<Country> countries;
        if (query != null) {
            countries = service.query(query, locale);
        } else if (values != null) {
            countries = service.query(values, locale);
        } else {
            countries = service.list(locale);
        }
        requestScope("countries", countries);
        return forward("index.jsp");
    }
}
